// pages/mysearch/mysearch.js
let schoolData = require('../../gis-school.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Height: 0,
    isRelation:false,
    scale: 13,
    latitude: "",
    longitude: "",
    markers: [],
    id:'',
    
    
    circles: [],
    valunteer:[],

    nearPeaple:[
      {
        name:"姓名",
        age:10,
        address:{
          latitude:0,
          longitude:0,
          addressName:"走失地点"
        },
        url:"../../images/per1.png",
        date:"2021/11/28",
        time:"20:00",
        type:"执行中",
        Peaple_id:"123"

      }
    ],
    passedClue:[],
    noPassClue:[],
    talk:[{
      speakerNickName:'讲话的人',
      speakerAvartar:'../../images/per1.png',
      speakTonickName:'被告知的人',
      time:"2021/1/11",
      content:'123456789'
    },{
      speakerNickName:'讲话的人',
      speakerAvartar:'../../images/per1.png',
      speakTonickName:'被告知的人',
      time:"2021/1/11",
      content:'123456789'
    },
    {
      speakerNickName:'讲话的人',
      speakerAvartar:'../../images/per1.png',
      speakTonickName:'被告知的人',
      time:"2021/1/11",
      content:'123456789'
    },{
      speakerNickName:'讲话的人',
      speakerAvartar:'../../images/per1.png',
      speakTonickName:'被告知的人',
      time:"2021/1/11",
      content:'123456789'
    }],
    interval :""
  },
  goToClue(e){
    console.log(e.currentTarget.dataset.clueid)
    var clueId = e.currentTarget.dataset.clueid
    wx.navigateTo({
      url: '../detailClue/detailClue?clueId='+clueId,
    })
  },
  pass(e){
    //通过 添加到上面的列表
    console.log(e.currentTarget.dataset.clueid)
    wx.request({
      url: app.globalData.Baseip+':8882/clue/confirmClue?clueId='+e.currentTarget.dataset.clueid,
     
      method:'POST',
      header: {
        Authorization:app.globalData.header
      },
      success:res=>{
       
        this.getPassedClue(this.data.id);
        
        wx.hideLoading({
          success: (res) => {},
        })
      
      },
      fail:function(err){
        console.log(err)
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  subClue(){
    wx.navigateTo({
      url: '../submitClue/submitClue?id='+this.data.id,
    })
  },
  noPass(e){
    //删除线索
    console.log(e.currentTarget.dataset.clueid)
    wx.request({
      
      url: app.globalData.Baseip+':8882/clue/repulseClue?clueId='+e.currentTarget.dataset.clueid,
     
      method:'POST',
      header: {
        Authorization:app.globalData.header
      },
      success:res=>{
       
        this.getPassedClue(this.data.id);
        
        wx.hideLoading({
          success: (res) => {},
        })
      
      },
      fail:function(err){
        console.log(err)
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })

  },
  
  getMap () {
    var _this = this;

    // var interval = setInterval(function () {  
    //   console.log("不断调用")
    //   //需不断调用的操作
    // }, 1000)

 

    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        
        _this.setData({
         
          circles: [{
            latitude: _this.data.latitude,
            longitude: _this.data.longitude,
            color: '#FF0000DD',
            fillColor: '#7cb5ec88',
            radius: 5000,
            strokeWidth: 0.5
          }]

        })
      }

    })

  },

  // regionchange(e) {
  //   // console.log("regionchange===" + e.type)
  // },
  // subText(e){
  //   console.log(e)
  // },
  //点击merkers
  markertap(e) {
    console.log(e.detail.markerId)
    console.log(e)
    for(var index in this.data.passedClue ){
      if(e.detail.markerId == this.data.passedClue[index].clue.id){
        // 如果是线索位置
        wx.navigateTo({
          url: '../detailClue/detailClue?clueId='+e.detail.markerId ,
        })
        break
      }
     
    }
    for(var index in this.data.valunteer ){
      if(e.detail.markerId == this.data.valunteer[index].volunteerId){
        // 如果是线索位置
        
          wx.showModal({
            title: '志愿者联系方式',
            content: this.data.phone,
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        
        
      }
    }
   
    
  },


  changeScale(){
    wx.navigateTo({
      url: '../changeScale/changeScale?id='+ this.data.id+'&loc='+this.data.address,
    })
  },
  inputs(e){
    console.log(e)
   
  },
  getPassedClue(situationId){
       // 获取线索
    wx.request({
      url: app.globalData.Baseip+':8882/clue/getSituationClue?situationId='+situationId,
     
      method:'GET',
      header: {
        Authorization:app.globalData.header
      },
      success:res=>{
        var data = res.data.data;
        console.log("clue",data)
        var passed=[];
        var noPassed=[];
        var markList = this.data.markers
        for(var i =0;i<data.length;i++){
          if(data[i].clue.confirm ==0){
            noPassed.push(data[i])
            // 生成mark数据
         
          }
          else if(data[i].clue.confirm == 1){
            passed.push(data[i])
        
            var mark = data[i].clue;
            mark.clueId = mark.id;
            mark.id = mark.clueId ;
            mark.width = 40;
            mark.height = 40;
            mark.iconPath='../../images/loc2.png';
            if(mark.latitude<-90 || mark.longitude<-90){//如果存在
              mark.latitude = 0
              mark.longitude= 0

            }
            markList.push(mark)
            // console.log('data',mark)
          }
          if(i == data.length-1){
            this.setData({
    
              markers:markList,
              passedClue:passed,
              noPassClue:noPassed
            })
          }
        }
        
        wx.hideLoading({
          success: (res) => {},
        })
      
      },
      fail:function(err){
        console.log(err)
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })


  },
  getIsRelation(){
    wx.request({
      url: app.globalData.Baseip+':8880/user/isRelation?situationId='+this.data.id,
     
      method:'GET',
      header: {
        Authorization:app.globalData.header
      },
      success:res=>{
        var is = res.data.data;
        // console.log("clue",is)
        this.setData({
          isRelation:is
        })
        wx.hideLoading({
          success: (res) => {},
        })
      
      },
      fail:function(err){
        console.log(err)
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })

  },
  //获取地图数据
  getMapData(){

    //获取位置
    
    var that = this;
    this.data.interval = setInterval(function () {  
      //获取志愿者信息
      wx.getLocation({
        type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          wx.request({
            url: app.globalData.Baseip+':8880/user/getVolunteerPosition?situationId='+that.data.id,
           
            method:'GET',
            header: {
              Authorization:app.globalData.header
            },
            success:res=>{
             
              console.log("Vlunteer",res)
              that.setData({
                valunteer:res.data.data
              })
              var markList = that.data.markers;
              // console.log(markList)
              for(var index = markList.length-1;index>=0;index--){
                // console.log("338",markList[index].volunteerId)
                
                if(markList[index].volunteerId != undefined){
                  // console.log('123')
                  markList.splice(index)
                }
              }
              var len = that.data.markers.length;
              var data = res.data.data
           
              for(var index in res.data.data){
                var mark = data[index];
                // console.log(data[index])
                mark.situationId = mark.volunteerId;
                mark.id = mark.volunteerId ;
                mark.width = 40;
                mark.height = 40;
                mark.iconPath='../../images/location.png';
                
                
                if(mark.latitude<-90 || mark.longitude<-90){//如果存在
                  mark.latitude = 0
                  mark.longitude= 0

                }
                markList.push(mark)
                // console.log(markList)
                if(index == data.length - 1){
                  // console.log(markList)
                  that.setData({
                    markers:markList
                  })
                }
                
              }
              
            },
            fail:function(err){
              // console.log(err)
              
            }
          })
          
        }
  
      })

      wx.getLocation({
        type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          wx.request({
            url: app.globalData.Baseip+':8880/user/updatePosition?latitude='+res.latitude+'&longitude='+res.longitude+'&address='+res.addressName,
           
            method:'POST',
            header: {
              Authorization:app.globalData.header
            },
            success:res=>{
             
              
            
            },
            fail:function(err){
              console.log(err)
              
            }
          })
          
        }
  
      })
    }, 30*1000)


    
  },
  goToDetail(){
    wx.navigateTo({
      url: '../personDetail/personDetail?situationId='+this.data.id,
    })
  },
  getDetail(id){
    wx.request({
      url: app.globalData.Baseip+':8881/situation/getSituationById?situationId='+id,
     
      method:'GET',
      header: {
        Authorization:app.globalData.header
      },
      success:res=>{
        var data = res.data.data;
        console.log(res)
       
        this.setData({
          
          age:data.age,
          createTime:data.createTime,
          address:data.lostAddress,
          latitude:data.lastFindLatitude,
          longitude:data.lastFindLongitude,
          name:data.name,
          beizu:data.other,
          phone:data.phone,
          sex:data.sex,
          status:data.status,
          album:data.album
        })
        
       
      },
      fail:function(err){
        console.log(err)
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    this.setData({
      id:options.id
    })
  },
  hasGet(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否已经找到',
      success (res) {
        if (res.confirm) {
          
          console.log(that.data)
          wx.request({
            url: app.globalData.Baseip+':8881/situation/alreadyFind?situationId='+that.data.id,
           
            method:'POST',
            header: {
              Authorization:app.globalData.header
            },
            success:res=>{
    
              console.log(res)
              wx.navigateTo({
                url: '../../pages/myhelp/myhelp',
              })
             
             
            },
            fail:function(err){
              console.log(err)
             
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

    
  },
  exitTeam(){
    
    wx.request({
      url: app.globalData.Baseip+':8881/situation/quitSituation?situationId='+this.data.id,
     
      method:'GET',
      header: {
        Authorization:app.globalData.header
      },
      success:res=>{
        var data = res.data.data;
        console.log(res)
       
        this.setData({
          
          age:data.age,
          createTime:data.createTime,
          address:data.lostAddress,
          latitude:data.lastFindLatitude,
          longitude:data.lastFindLongitude,
          name:data.name,
          beizu:data.other,
          phone:data.phone,
          sex:data.sex,
          status:data.status,
          album:data.album
        })
        
       
      },
      fail:function(err){
        console.log(err)
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.mapCtx = wx.createMapContext('myMap');
 
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPassedClue(this.data.id);
    this.getDetail(this.data.id);
    this.getMap();
    this.getIsRelation();
    this.getMapData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.interval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})